<template>
    <el-form v-loading="loading" ref="ruleForm" :model="ruleForm" :rules="rules">
        <el-row :gutter="1">
            <el-col :span="24">
                <el-form-item label="交易签名：" prop="code" :label-width="formLabelWidth3">
                    <el-input size="small" v-model="ruleForm.code" type="textarea" :rows="4"></el-input>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row :gutter="1">
            <el-col :span="24">
                <el-form-item label="" :label-width="formLabelWidth3">
                    <el-button type="primary" size="small" @click="deCode">解码</el-button>
                    <el-button type="primary" size="small" @click="broadcast">广播交易</el-button>
                    <el-button type="primary" size="small" @click="seecast">查看交易</el-button>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row :gutter="1" style="margin-top: 20px">
            <el-col :span="24">
                <el-form-item label=" 解码结果：" prop="title" :label-width="formLabelWidth3">
                    <div style="white-space: pre-wrap; word-wrap: break-word; font-size: 12px; color: #606266; line-height: 24px;" id="resultData">
                        {{ resultData }}
                    </div>
                </el-form-item>
            </el-col>
        </el-row>
    </el-form>
</template>

<script>
    import Fetch from 'Fetch';
    export default {
        name: "Autograph",
        data() {
            return {
                loading: false,
                formLabelWidth1: '60px',
                formLabelWidth2: '80px',
                formLabelWidth3: '86px',
                ruleForm: {
                    code: ''
                },
                url: '/tx/decodetx',
                rules: {
                    code: [
                        {required: true, message: '请输入签名', trigger: ['blur', 'change']}
                    ],
                },
                resultData: null,
                resultHash: null
            }
        },
        props: ['proParm'],
        methods: {
            async deCode() {
                let self = this;
                this.$refs['ruleForm'].validate(async (valid) => {
                    if (valid) {
                        this.loading = true;
                        this.newAddress = [];
                        let result = await Fetch.Get(this.url, {rawhex: this.ruleForm.code});
                        this.loading = false;
                        if (result.code === '0') {
                            result.data.vout.forEach((item, index) => {
                                if(item.addresses){
                                    item.addresses.forEach((item1, index1) => {
                                        result.data.vout[index].addresses[index1] = addressConvert(self.proParm, item1);
                                    });
                                }
                            });
                            self.resultData = JSON.stringify(result.data, null, 2);
                            self.resultHash = result.data.hash;
                        }
                        else {
                            self.resultData = '';
                            self.resultHash = '';
                            this.$message.error(result.msg);
                        }
                    }
                    else {
                        return false;
                    }
                });
            },
            broadcast(){
                if(!this.resultHash){
                    this.$message({
                        type: 'warning',
                        message: '请先进行解码'
                    });
                    return false;
                }
                this.$confirm('是否已经核对交易签名解码之后的数据?', '提示', {
                    confirmButtonText: '是',
                    cancelButtonText: '否',
                    type: 'warning'
                }).then( async () => {
                    let result = await Fetch.Get('/tx/publishtx',{ cointype: this.proParm, rawhex: this.ruleForm.code });
                    if(result.code === '0'){
                        this.$message({
                            type: 'success',
                            message: result.msg
                        });
                    }
                    else {
                        this.$message({
                            type: 'error',
                            message: result.msg
                        });
                    }
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消'
                    });
                });
            },
            seecast(){
                if(!this.resultHash){
                    this.$message({
                        type: 'warning',
                        message: '请先进行解码'
                    });
                    return false;
                }
                switch (this.proParm) {
                    case 'bch':
                        window.open("https://blockchair.com/bitcoin-cash/transaction/"+this.resultHash);
                        break;
                    case 'btc':
                        window.open("https://blockchair.com/bitcoin/transaction/"+this.resultHash);
                        break;
                    case 'fch':
                        window.open("https://freecash.info/transaction?hash="+this.resultHash);
                        break;
                    default:
                        break;
                }
            }
        }
    }
</script>

<style scoped lang="less">
    /deep/ .el-form-item__label{
        padding: 0 5px 0 0;
    }
</style>
